//
//  BaiduRouteTableViewController.m
//  WisdomZhuHai
//
//  Created by Ryan on 15-1-30.
//  Copyright (c) 2015年 wyd. All rights reserved.
//

#import "BaiduRouteTableViewController.h"
#import "BaiduMapViewController.h"
#import "Common.h"
#import "TransforController.h"
#import "ZFModalTransitionAnimator.h"
#import "BaiduRouteDetailTableViewController.h"

@interface BaiduRouteTableViewController () <BaiduMapNavDelegate>

@property (nonatomic, retain)UITableView* tableview;

@property (nonatomic, strong) ZFModalTransitionAnimator *animator;
@property BOOL dragable;

@end

@implementation BaiduRouteTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.tableview.backgroundColor = [Common colorWithRGB:0xf7f7f7];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:[TransforController shardeInstanceOfNavigationBarAndStatusBarCoverViewWithY:0 andTitle:@"规划方案"]];
    [self.view addSubview:self.tableview];
    self.dragable = NO;
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)blankCellSetup:(UITableViewCell*)cell index:(NSInteger)index {
    if (index % 2 ==0) {
        UIView *middle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
        middle.backgroundColor = [Common colorWithRGB:0xf7f7f7];
        [cell.contentView addSubview:middle];
    }
}

- (void)cellSetup:(UITableViewCell*)cell index:(NSInteger)index {
    NSLog(@"%lu",(unsigned long)[_busResult.routes count]);
    int tempIndex = (index - 1) / 2;
    BMKTransitRouteLine *line = (BMKTransitRouteLine*)[_busResult.routes objectAtIndex:tempIndex];
    NSMutableArray *busArray = [[NSMutableArray alloc] init];
    float distance = line.distance / 1000.0;
    NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *distanceString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:distance]];
    NSLog(@"距离：%@",distanceString);
    NSLog(@"%d小时%d分钟：",line.duration.hours, line.duration.minutes);
    NSString *bus = nil;
    for (int i = 0; i < [line.steps count]; i++) {
        BMKTransitStep* transitStep = [line.steps objectAtIndex:i];
        bus = transitStep.vehicleInfo.title;
        if (![bus isEqualToString:@""] && bus.length > 0 && bus != nil) {
            [busArray addObject:bus];
        }
    }
    NSLog(@"公交：%@",busArray);
    UILabel *planNum = (UILabel*)[cell viewWithTag:1001];
    planNum.text = [NSString stringWithFormat:@"%d", tempIndex + 1];
    [self createCell:cell busArray:busArray hours:line.duration.hours minutes:line.duration.minutes distance:distanceString];
}

- (void)createCell:(UITableViewCell*)cell busArray:(NSArray*)busArray hours:(int)hours minutes:(int)minutes distance:(NSString*)distance {
    UIView *busView = [cell viewWithTag:1003];
    UIView *time_km = [cell viewWithTag:1004];
    CGRect frame;
    float busX = 0;
    float arrowX = 0;
    for (int i = 0; i < busArray.count; i++) {
        UILabel *busLab = [[UILabel alloc] initWithFrame:CGRectMake(busX, 0, 50, 25)];
        busLab.font = [UIFont fontWithName:@"Arial" size:16.0];
        busLab.textColor = [Common colorWithRGB:0x4c4c4c];
        busLab.text = busArray[i];
        [busLab sizeToFit];
        frame = busLab.frame;
        [busView addSubview:busLab];
        busX += frame.size.width + 20;
        if (i < busArray.count - 1) {
            arrowX = busLab.frame.origin.x + busLab.frame.size.width + 2;
            UIImageView *arrowView = [[UIImageView alloc] initWithFrame:CGRectMake(arrowX, 5, 15, 7)];
            arrowView.image = [UIImage imageNamed:@"map/arrow"];
            [busView addSubview:arrowView];
        }
    }
    UILabel *time_km_lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 20)];
    time_km_lab.font = [UIFont fontWithName:@"Arial" size:13.0];
    time_km_lab.textColor = [Common colorWithRGB:0x999999];
    if (hours <= 0) {
        time_km_lab.text = [NSString stringWithFormat:@"%d分钟/%@km", minutes ,distance];
    } else {
        time_km_lab.text = [NSString stringWithFormat:@"%d小时%d分钟/%@km", hours, minutes ,distance];
    }
    [time_km addSubview:time_km_lab];
}

- (void)driverCellSetup:(UITableViewCell*)cell index:(NSInteger)index {
    NSLog(@"%lu",(unsigned long)[_driverResult.routes count]);
    int tempIndex = (index - 1) / 2;
    BMKDrivingRouteLine *line = (BMKDrivingRouteLine*)[_driverResult.routes objectAtIndex:tempIndex];
    float distance = line.distance / 1000.0;
    NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *distanceString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:distance]];
    NSLog(@"距离：%@",distanceString);
    NSLog(@"%d小时%d分钟：",line.duration.hours, line.duration.minutes);
    NSLog(@"%@",line.title);
    NSArray *targetArray = [NSArray arrayWithObjects:@"我的位置", _name, nil];
//    for (int i = 0; i < [line.steps count]; i++) {
//        BMKDrivingStep* transitStep = [line.steps objectAtIndex:i];
//        NSLog(@"%@",transitStep.instruction);
//        NSLog(@"%@",transitStep.entraceInstruction);
//    }
    UILabel *planNum = (UILabel*)[cell viewWithTag:1001];
    planNum.text = [NSString stringWithFormat:@"%d", tempIndex + 1];
    UIImageView *carImgView = (UIImageView*)[cell viewWithTag:1002];
    carImgView.image = [UIImage imageNamed:@"map/half_car"];
    [self createCell:cell busArray:targetArray hours:line.duration.hours minutes:line.duration.minutes distance:distanceString];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_is_bus) {
        return [_busResult.routes count] * 2;
    } else {
        return [_driverResult.routes count] * 2;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 0) {
        return 10;
    }
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = nil;
    if (indexPath.row % 2 == 0) {
        static NSString *CellIdentifier = @"SeasonCell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        [self blankCellSetup:cell index:indexPath.row];
        
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"BaiduRouteCell"];
        // Configure the cell...
        if(cell == nil)
        {
            cell = [[NSBundle mainBundle] loadNibNamed:@"BaiduRouteCell"  owner:self options:nil][0];
        }
        if (_is_bus == YES) {
            [self cellSetup:cell index:indexPath.row];
        } else {
            [self driverCellSetup:cell index:indexPath.row];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 != 0) {
        NSLog(@"%s",__FUNCTION__);
        BaiduRouteDetailTableViewController *baiduRouteDetailCtrl = [[[BaiduRouteDetailTableViewController alloc] init] autorelease];
        baiduRouteDetailCtrl.modalPresentationStyle = UIModalPresentationCustom;
        
        self.animator = [[ZFModalTransitionAnimator alloc] initWithModalViewController:baiduRouteDetailCtrl];
        self.animator.dragable = self.dragable; //是否滑动返回主页
        self.animator.bounces = NO;
        self.animator.behindViewAlpha = 0.5f;
        self.animator.behindViewScale = 1.0f;
        self.animator.positionY = [UIScreen mainScreen].bounds.size.height / 3;
        baiduRouteDetailCtrl.is_bus = _is_bus;
        baiduRouteDetailCtrl.name = [NSString stringWithFormat:@"%@", _name];
        if (_is_bus) {
            int tempIndex = (indexPath.row - 1) / 2;
            BMKTransitRouteLine *line = (BMKTransitRouteLine*)[_busResult.routes objectAtIndex:tempIndex];
            baiduRouteDetailCtrl.busLine = line;
        } else {
            int tempIndex = (indexPath.row - 1) / 2;
            BMKDrivingRouteLine *line = (BMKDrivingRouteLine*)[_driverResult.routes objectAtIndex:tempIndex];
            baiduRouteDetailCtrl.carLine = line;
        }
        baiduRouteDetailCtrl.index = (indexPath.row - 1 ) / 2;
        baiduRouteDetailCtrl.mapDelegate = self;
        baiduRouteDetailCtrl.transitioningDelegate = self.animator;
        baiduRouteDetailCtrl.hidesBottomBarWhenPushed = YES;
        [self presentViewController:baiduRouteDetailCtrl animated:YES completion:nil];

    }
}

- (void) beginBaiduMapNav:(NSInteger)index {
    NSLog(@"%s , %d", __FUNCTION__, index);
    [self.navigationController popViewControllerAnimated:YES];
    [self.mapNavDelegate beginToNav:index busResult:_busResult carResult:_driverResult];
}


@end
